package com.szt.mobile.inventory.modules.main.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Switch;
import android.widget.TextView;

import com.handset.pakage.UHFTag;
import com.isoftstone.mis.ffair.util.log.FFLog;
import com.szt.mobile.inventory.R;
import com.szt.mobile.inventory.app.HandsetApplication;
import com.szt.mobile.inventory.common.ui.BaseActivity;
import com.szt.mobile.inventory.common.utils.SpUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by yanzhaodi on 2017/9/25.
 */

public class PowerSettingActivity extends BaseActivity {

    @BindView(R.id.rb_power_setting_max)
    RadioButton maxRb;

    @BindView(R.id.rb_power_setting_min)
    RadioButton minRb;

    @BindView(R.id.ll_power_setting_max)
    LinearLayout maxLl;

    @BindView(R.id.ll_power_setting_min)
    LinearLayout minLl;

    @BindView(R.id.tv_power_setting_text)
    TextView textResultTv;

    @BindView(R.id.et_power_setting_max)
    EditText maxPowerEt;

    @BindView(R.id.et_power_setting_min)
    EditText minPowerEt;
    @BindView(R.id.btn_switch)
    Switch btn_switch;

    private int max;
    private int min;

    private boolean isMax = true;

    private HandsetApplication application;
    private static volatile boolean bGetEPC;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_power_setting);
        ButterKnife.bind(this);

        application = ((HandsetApplication) getApplication());

        max = SpUtil.getInstance(this).getKeyIntValue(SpUtil.Key.KEY_MAX_POWER, 23);
        min = SpUtil.getInstance(this).getKeyIntValue(SpUtil.Key.KEY_MIN_POWER, 23);

        maxRb.setChecked(true);
        maxPowerEt.setText(String.valueOf(max));
        minPowerEt.setText(String.valueOf(min));

        maxRb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    maxLl.setVisibility(View.VISIBLE);
                    minLl.setVisibility(View.GONE);
                } else {
                    maxLl.setVisibility(View.GONE);
                    minLl.setVisibility(View.VISIBLE);
                }

                isMax = isChecked;
            }
        });

        btn_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    SpUtil.getInstance(PowerSettingActivity.this).saveKeyStringValue("switch", "on");
                } else {
                    SpUtil.getInstance(PowerSettingActivity.this).saveKeyStringValue("switch", "off");
                }
            }
        });
        if (SpUtil.getInstance(PowerSettingActivity.this).getKeyStringValue("switch", "").equals("on")) {
            btn_switch.setChecked(true);
        } else {
            btn_switch.setChecked(false);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        // 开始扫描RFID
        if (application.checkServiceAndDevice()) {
            application.device.uhfPowerOn();
            application.device.setUhfPower(max * 10, 200, 300);
            bGetEPC = false;
            new GetRFIDThread().start();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        if (application.checkServiceAndDevice()) {
            application.device.uhfPowerOff();
        }

        bGetEPC = false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @OnClick(R.id.btn_power_setting_text)
    public void text() {
        int curPower = 23;
        if (isMax) {
            String textMax = maxPowerEt.getText().toString().trim();
            if (TextUtils.isEmpty(textMax)) {
                showToast("请输入功率");
                return;
            } else {
                max = Integer.valueOf(textMax);
                curPower = max;
            }
        } else {
            String textMin = minPowerEt.getText().toString().trim();
            if (TextUtils.isEmpty(textMin)) {
                showToast("请输入最小功率");
                return;
            } else {
                min = Integer.valueOf(textMin);
                curPower = min;
            }
        }

        if (max < 5 || max > 28) {
            showToast("功率范围为5到28");
            return;
        }

        // 开始扫描RFID
        application.device.setUhfPower(curPower * 10, 200, 300);
    }

    private class GetRFIDThread extends Thread {
        @Override
        public void run() {
            bGetEPC = true;
            while (bGetEPC) {
                try {
                    sleep(100);
                } catch (InterruptedException e) {
                    FFLog.e(e, e.getMessage());
                }

                try {
                    int cnt = application.device.getSize();
                    FFLog.i("--->cnt = " + cnt);

                    for (int i = 0; i < cnt; i++) {
                        UHFTag uhfTag = application.device.getItem();

                        if (uhfTag == null) {
                            continue;
                        }

                        String epc = uhfTag.getEpc(true).replace(" ", "");
                        final String rfid = epc.substring(epc.length() - 8, epc.length());

                        FFLog.i("--->rfid:", rfid);

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                textResultTv.setText(rfid);
                            }
                        });

                    }
                } catch (Exception e) {
                    FFLog.e(e, e.getMessage());
                }
            }
        }
    }

    @OnClick(R.id.btn_power_setting_save)
    public void save() {
        String textMax = maxPowerEt.getText().toString().trim();
        if (TextUtils.isEmpty(textMax)) {
            showToast("请输入功率");
            return;
        }

        max = Integer.parseInt(textMax);

        if (max < 5 || max > 28) {
            showToast("功率范围为5到28");
            return;
        }

        SpUtil.getInstance(this).saveKeyIntValue(SpUtil.Key.KEY_MAX_POWER, max);
        SpUtil.getInstance(this).saveKeyIntValue(SpUtil.Key.KEY_MIN_POWER, min);

        finish();
    }
}
